package com.cinema;

import java.io.IOException;
import java.util.Calendar;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import query.DataQuery;

import com.cinema.data.PMF;
import com.cinema.data.User;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


public class Register extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Register(){
		super();
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req, resp);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String username = request.getParameter("ftp-user-name");
		String password = request.getParameter("ftp-password");
		String email = request.getParameter("email");
		String fullname = request.getParameter("fullname");
		String sex = request.getParameter("sex");
		String phoneno = request.getParameter("sdt");
		//int birthday = Integer.parseInt(request.getParameter("birthday"));
		//int birthmonth = Integer.parseInt(request.getParameter("birthmonth"));
		int birthyear = Integer.parseInt(request.getParameter("NS"));
		Calendar cal = Calendar.getInstance();
		int age = cal.get(Calendar.YEAR) - 1900 - birthyear;
		
		/*Entity user = new Entity("User", username);
		user.setProperty("username", username);
		user.setProperty("password", password);
		user.setProperty("fullname", fullname);
		user.setProperty("email", email);
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(user);*/
		User usr = new User(username, password, email, age, fullname, sex, phoneno);
		DataQuery query = new DataQuery();
		boolean result = query.addUser(usr);
		if(result){
			HttpSession session = request.getSession();
			session.setAttribute("user", usr);
			response.sendRedirect("index.jsp");
		}else{
			response.sendRedirect("register.html");
		}
	}
}
